package com.qb.main.ui.adapter

import android.R.attr
import android.view.View
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.SizeUtils
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.qb.common.bean.ImageBean
import com.qb.common.widget.GifWebView
import com.qb.main.R


class ImageAdapter:BaseQuickAdapter<ImageBean,BaseViewHolder>(R.layout.image_item) {
    override fun convert(holder: BaseViewHolder, item: ImageBean) {
        holder.setText(R.id.tv_title,"${item.title}")
            .setText(R.id.tv_from,"来源：${item.from}")
            .setText(R.id.tv_good,"好评：${item.good}")
            .setText(R.id.tv_bad,"差评：${item.bad}")
        var iv_image = holder.getView<ImageView>(R.id.iv_png)
        var iv_gif = holder.getView<GifWebView>(R.id.iv_gif)
        var tv_joke_content = holder.getView<TextView>(R.id.tv_joke_content)
        iv_image.visibility = View.GONE
        iv_gif.visibility = View.GONE
        tv_joke_content.visibility = View.GONE
        if (item.img.startsWith("http")){
            iv_gif.visibility = View.VISIBLE
            iv_gif.loadGifUrl(item.img)
        }else{
            tv_joke_content.visibility = View.VISIBLE
            tv_joke_content.text = "${item.img}"
        }
//        if (item.img.contains(".gif")){
//            iv_gif.visibility = View.VISIBLE
//            iv_gif.loadGifUrl(item.img)
//        }else {
//            try {
//                var index = item.img.indexOf("t_s")
//                var end = item.img.indexOf("/g")
//                var result = item.img.substring(index+3,end)
//                var width = result.split("x")[0].toInt()
//                var height = result.split("x")[1].toInt()
//                LogUtils.e("输出截取的图片宽高:$result")
//                val params = LinearLayout.LayoutParams(SizeUtils.px2dp(width.toFloat()), SizeUtils.px2dp(height.toFloat()))
//                iv_image.layoutParams = params
//                iv_image.visibility = View.VISIBLE
//                Glide.with(context).load(item.img).into(iv_image)
//            } catch (e: Exception) {
//                val params = LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)
//                iv_image.layoutParams = params
//                iv_image.visibility = View.VISIBLE
//                Glide.with(context).load(item.img).into(iv_image)
//            }
//        }
//        Glide.with(context)
//            .asGif()
//            .load(item.img)
//            .apply(
//                RequestOptions()
//                .placeholder(R.mipmap.ic_launcher) // 可选
//                .error(R.mipmap.ic_launcher)) // 可选
//            .transition(DrawableTransitionOptions.withCrossFade())
//            .into(iv_img)
//        imageView.load(item.img){
//            crossfade(true)
//            placeholder(R.mipmap.ic_launcher)
//            transformations(CircleCropTransformation())
//        }
        // 创建一个支持 GIF 的 ImageLoader
//        val imageLoader = ImageLoader.Builder(context)
//            .componentRegistry {
//                add(GifDecoder())
//            }
//            .build()
//
//        // 使用 ImageRequest 加载 GIF
//        val request = ImageRequest.Builder(context)
//            .data("https://xiaohua-fd.zol-img.com.cn/t_s300x2000/g7/M00/0F/0C/ChMkK2QYLLyILNcBAAA1qwbKqAgAAN-MwIV3wcAADXD373.jpg")
//            .target(imageView)
//            .placeholder(R.mipmap.ic_launcher)
//            .crossfade(true)
//            .build()
//
//        // 执行请求
//        imageLoader.enqueue(request)
    }
}